package org.mx.spring.config;

import org.mx.spring.session.SessionDataStore;
import org.mx.spring.session.impl.SessionDataThreadLocal;
import org.mx.spring.task.TaskFactory;
import org.mx.spring.time.SystemClock;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Spring基础组件配置类
 * 
 * @author john.peng
 *
 */
@Configuration
public class SpringUtilsAutoConfiguration {
	@Value("${system.clock.precision: 1}")
	private long precision;
	
	/**
	 * 创建SessionDataStore服务
	 * 
	 * @return SessionDataStore
	 */
	@ConditionalOnClass({SessionDataThreadLocal.class})
	@Bean
	public SessionDataStore sessionDataStore() {
		return new SessionDataThreadLocal();
	}

	/**
	 * 创建一个任务工厂服务
	 * 
	 * @return TaskFactolry
	 */
	@ConditionalOnClass({TaskFactory.class})
	@Bean(name = "taskFactory", destroyMethod = "shutdown")
	public TaskFactory taskFactory() {
		return new TaskFactory();
	}

	/**
	 * 创建一个系统时钟服务
	 * 
	 * @param taskFactory TaskFactory服务
	 * @return SystemClock
	 */
	@ConditionalOnClass({SystemClock.class})
	@Bean(name = "systemClock", initMethod = "init", destroyMethod = "close")
	public SystemClock systemClock(TaskFactory taskFactory) {
		return new SystemClock(precision, taskFactory);
	}
}
